package cn.edu.nks.dormitory.student.editinfo.action;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;

import cn.edu.nks.dormitory.model.StudentInfo;
import cn.edu.nks.dormitory.student.editinfo.db.IDBService;
import cn.edu.nks.dormitory.student.editinfo.dbimpl.DBService;
/**
 * 学生查看个人信息
 * @author wangbs
 *
 */
public class ShowPersonInfoAction extends HttpServlet{
	
	private static final long serialVersionUID = 1L;
	private static Logger logger = Logger.getLogger(ShowPersonInfoAction.class);
	private IDBService idbService = new DBService();
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doPost(req, resp);
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		resp.setContentType("application/jsons;charset=utf-8");
		String userId = req.getParameter("userId");
		if(userId == null || userId.isEmpty()){
			logger.warn("user id is null");
			return;
		}
		StudentInfo studentInfo = idbService.getStudentInfoByUserId(Integer.valueOf(userId));
		if(studentInfo == null){
			logger.warn("student info is null");
		}
		logger.warn(String.format("show student info :%s", studentInfo.toString()));
		PrintWriter out = resp.getWriter();
		out.write(JSONObject.fromObject(studentInfo).toString());
	}
}
